package com.jiaoyou.baojian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jiaoyou.baojian.R;
import com.jiaoyou.baojian.adapter.MineAdapter;
import com.jiaoyou.baojian.entity.Signin;
import com.jiaoyou.baojian.entity.UserDetail;
import com.jiaoyou.baojian.http.Api;
import com.jiaoyou.baojian.http.BaofangCallback;
import com.jiaoyou.baojian.otto.AppBus;
import com.jiaoyou.baojian.otto.Event;
import com.jiaoyou.baojian.utils.Utils;
import com.squareup.otto.Subscribe;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;

import static com.jiaoyou.baojian.otto.Event.EVENT_REFRESH_USERINFO;

public class MineActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.header)
    ImageView header;
    @BindView(R.id.username)
    TextView username;
    @BindView(R.id.type)
    ImageView type;
    @BindView(R.id.gongxian)
    TextView gongxian;
    @BindView(R.id.money)
    TextView money;
    @BindView(R.id.chongzhi)
    Button chongzhi;
    @BindView(R.id.guanzhusum)
    TextView guanzhusum;
    @BindView(R.id.userinfo)
    LinearLayout userinfo;
    @BindView(R.id.guanzhu)
    LinearLayout guanzhu;
    @BindView(R.id.fensisum)
    TextView fensisum;
    @BindView(R.id.fensi)
    LinearLayout fensi;
    @BindView(R.id.attractivenesssum)
    TextView attractivenesssum;
    @BindView(R.id.attractiveness)
    LinearLayout attractiveness;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;

    private MineAdapter mineAdapter;
    private ArrayList<String> mines = new ArrayList<>();

    private UserDetail userDetail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ButterKnife.bind(this);

        setTitle("我");

        needOtto();

        init();
    }

    @Override
    public int getContentView() {
        return R.layout.activity_mine;
    }

    public void init() {
        fillData();

        Api.getInstance().getCurrentUserInfo(new BaofangCallback<UserDetail>() {
            @Override
            public void onFailure(Call call, Throwable t) {

            }

            @Override
            public void onSuccess(UserDetail userDetail) {
                getLoginResponse().update(MineActivity.this, userDetail);
                MineActivity.this.userDetail = userDetail;
                fillData();
            }
        });
        mines = new ArrayList<>();
        recyclerview.setLayoutManager(new GridLayoutManager(this, 4));
        mineAdapter = new MineAdapter();
        recyclerview.setAdapter(mineAdapter);
        mineAdapter.setOnItemClickListener(this);
        mines.add("签到");
        mineAdapter.addIcon(R.drawable.btn_check_selector);
        if(getLoginResponse().actress) {
            mines.add("创建包房");
            mineAdapter.addIcon(R.drawable.btn_create_selector);
        }
        if(!TextUtils.isEmpty(getLoginResponse().userInfo.sex) && getLoginResponse().userInfo.sex.equals("F") && !getLoginResponse().actress) {
            mines.add("成为声优");
            mineAdapter.addIcon(R.drawable.btn_application_selector);
        }
        mines.add("钱包");
        mineAdapter.addIcon(R.drawable.btn_wallet_selector);
        mines.add("消息");
        mineAdapter.addIcon(R.drawable.btn_news_selector);
        mines.add("反馈");
        mineAdapter.addIcon(R.drawable.btn_feedback_selector);
        mines.add("设置");
        mineAdapter.addIcon(R.drawable.btn_setting_selector);
        mineAdapter.setNewData(mines);
        mineAdapter.notifyDataSetChanged();
    }

    public void fillData(){
        if(!TextUtils.isEmpty(getLoginResponse().userInfo.nickname)
                && !TextUtils.isEmpty(getLoginResponse().userInfo.birthday))
            username.setText(getLoginResponse().userInfo.nickname + "，" + Utils.getAge(getLoginResponse().userInfo.birthday));
        gongxian.setText("贡献值：" + getLoginResponse().user.contribution);
        Utils.showCircleImage(this, getLoginResponse().userInfo.avatar, header);
        Utils.showShengyouTypeImage(this, getLoginResponse().userInfo.femaleType, type);

        if(userDetail != null) {
            guanzhusum.setText("" + userDetail.favCount);
            fensisum.setText("" + userDetail.fansCount);
            attractivenesssum.setText("" + userDetail.user.attractiveness);
            money.setText("我的余额：" + userDetail.card.getTotalAmount() + " 豆");
        }
    }

    @OnClick({R.id.chongzhi, R.id.guanzhu, R.id.fensi, R.id.userinfo})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.chongzhi:
                RechargeActivity.show(this);
                break;
            case R.id.guanzhu: {
                Intent intent = new Intent(this, FollowActivity.class);
                intent.putExtra("userid", getLoginResponse().userInfo.userId);
                startActivity(intent);
            }
                break;
            case R.id.fensi: {
                Intent intent = new Intent(this, FansActivity.class);
                intent.putExtra("userid", getLoginResponse().userInfo.userId);
                startActivity(intent);
            }
                break;
            case R.id.userinfo:
                startActivity(EditUserInfoActivity.class);
                break;
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if(position == 0){
            waiting("签到中...");
            String token = userDetail.signinToken;
            Api.getInstance().signIn(token, new BaofangCallback<Signin>() {
                @Override
                public void onFailure(Call call, Throwable t) {
                    stopWaiting();
//                    toast("签到失败：" + t.getLocalizedMessage());
                    toast("" + t.getLocalizedMessage());
                }

                @Override
                public void onSuccess(Signin response) {
                    stopWaiting();
                    if(response == null) {
                        toast("签到失败");
                        return;
                    }
                    toastFullscreen("签到成功，获得" + response.candyGiven + "豆");
                    AppBus.getInstance().post(Event.EVENT_REFRESH_USERINFO);
                }
            });
        } else if(position == (mines.size() - 4)){
            if(getLoginResponse().actress){
                startActivity(WalletZhuboActivity.class);
            } else {
                startActivity(WalletActivity.class);
            }
        } else if(position == (mines.size() - 3)){
            startActivity(NewsActivity.class);
        } else if(position == (mines.size() - 2)){
            startActivity(FeedBackActivity.class);
        } else if(position == (mines.size() - 1)){
            startActivity(SettingActivity.class);
        } else if(getLoginResponse().actress && position == 1){
            Intent intent = new Intent(this, CreateBaofangActivity.class);
            intent.putExtra("room", getLoginResponse().lastClosedPublicRoom);
            startActivity(intent);
        } else if(getLoginResponse().userInfo.sex.equals("F") && !getLoginResponse().actress && position == 1){
            if(userDetail.userInfo.imageList == null || userDetail.userInfo.imageList.size() == 0) {
                toast("请先上传美照哦");
                return;
            }
            startActivity(TobeZhuboActivity.class);
        }
    }

    @Subscribe
    public void onBusEvent(String event){
        if(EVENT_REFRESH_USERINFO.equals(event)){
            init();
        }
    }
}
